package it.uniroma2.monitor.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class MetricResult {

	private String name;
	private Collection<MetricValue> values;

	public MetricResult(String name) {
		this.name = name;
		values = new ArrayList<MetricValue>();
	}

	public void add(MetricValue metricValue) {
		values.add(metricValue);
	}

	public int size() {
		if (values != null)
			return values.size();
		else
			return 0;
	}

	public double avg() {
		double sum = 0D;
		double count = 0D;
		for (MetricValue v : values) {
			if (v.getValue() != Double.NaN) {
				count++;
				sum += v.getValue();
			}
		}
		return sum / count;
	}

	public double last() {
		final Iterator<MetricValue> itr = values.iterator();
		MetricValue lastElement = itr.next();
		while (itr.hasNext()) {
			lastElement = itr.next();
		}
		return lastElement.getValue();
	}

}
